package com.prizedraw.prize_draw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prizedraw.prize_draw.entity.DepartmentRole;
import com.prizedraw.prize_draw.entity.UserDepartmentRole;
import com.prizedraw.prize_draw.mapper.UserDepartmentRoleMapper;
import com.prizedraw.prize_draw.service.DepartmentRoleService;
import com.prizedraw.prize_draw.service.UserDepartmentRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-09-07
 */
@Service
public class UserDepartmentRoleServiceImpl extends ServiceImpl<UserDepartmentRoleMapper, UserDepartmentRole> implements UserDepartmentRoleService {

    @Autowired
    DepartmentRoleService departmentRoleService;

    @Override
    public void save(Integer userId, List<DepartmentRole> departmentRoles) {
        departmentRoles.forEach(dr -> {
            // 查询部门id和岗位id在关联表中的id
            DepartmentRole departmentRole = departmentRoleService.getOne(new QueryWrapper<DepartmentRole>().eq("department_id", dr.getDepartmentId()).eq("role_id", dr.getRoleId()));
            // 封装成一个UserDepartmentRole对象
            UserDepartmentRole userDepartmentRole = new UserDepartmentRole();
            userDepartmentRole.setUserId(userId);
            userDepartmentRole.setDepartmentRoleId(departmentRole.getId());
            // 将用户id与部门岗位id的UserDepartmentRole对象进行保存关联
            this.save(userDepartmentRole);
        });
    }
}
